package cn.edu.gench.learn.controller;

import cn.edu.gench.learn.model.Book;
import cn.edu.gench.learn.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;
import java.util.List;

/**
 * Created by Administrator on 2017/4/28.
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;


    /**
     *
     *  1  查找所有的书
     *  select * from d8_book;
     *  2  查找一本书
     *  select * from d8_booke where id = xx and name = ''
     *  3 修改一本书
     *  update d8_book set name = 'xxx' where  id = xx
     *  4 删除一本书
     *  delete from 8_book where  id = xx
     *
     *
     */

    @ResponseBody
    @RequestMapping("/list")
    public List<Book> getAllBooks(){
        return bookService.getBooks();
    }


    /***
     * /book/show?id=1
     *
     * 根据ID 获得一本书的信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/show")
    public Book show(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        return bookService.getBookById(id);
    }




    /***
     * /book/update?id=1&name=中华
     *
     * 根据ID 获得一本书的信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public Book update(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        return bookService.update(id,name);
    }


    @ResponseBody
    @RequestMapping("/delete")
    public Book delete(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        return bookService.delete(id);
    }




    @ResponseBody
    @RequestMapping("/test")
    public String test(HttpServletRequest request, HttpServletResponse responsez){
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        String name = request.getParameter("name");
        System.out.println(MessageFormat.format("controller=> id = {0} ,name = {1}",id,name));
        return bookService.test(id,name);
    }
























}
